// 全局插件
import axios from 'axios'
import * as monaco from "monaco-editor";
import('monaco-themes/themes/Monokai.json').then(data=>{monaco.editor.defineTheme('monokai',data)})
import('monaco-themes/themes/Solarized-light.json').then(data=>{monaco.editor.defineTheme('solarlight',data)})
import('monaco-themes/themes/GitHub Light.json').then(data=>{monaco.editor.defineTheme('githublight',data)})
const prefix = "http://localhost:8080/yd"
// 需要使用call来进行调用
// 需要实现的功能 可选式的传入success函数 最后都返回一个promise 进行后续的操作
// 不管传不传success 都返回一个promise进行后续操作
// 不传success就空执行
// 接口对应的是axios的一个promise对象
// 成功的话 返回fullied状态的promise 否则返回rejected的promise
// code!=200 默认为fullied状态
const basicAxios = function(promise,
    success=()=>{}, // 空执行
    error=(resp)=>{
        this.notify('error',resp.data.msg)
        switch(resp.data.code){
            case 403: // token失效错误码 跳转登录界面
                this.$router.push({
                    name:'login'
                })
                break;
        }
    }, // 错误的执行
    networkerror=(resp)=>{this.notify('error',`[ ${resp.name} ] ${resp.message}`)}
    ){
    return promise.then((resp)=>{
        let i;
        if(resp.data.code!=200){
            // 执行错误回调
            i = error(resp)
        }else{
            //执行正确回调
            i = success(resp)
        }
        return Promise.resolve(i)
    },(resp)=>{
        const i = networkerror(resp)
        return Promise.reject(i)
    })
}
const getUserInfo =(token)=>{
    return axios.get(prefix+'/user/getUserMsg',{
    headers:{
        yToken:token
}})
}
const userLogin = function(user){
    return axios.post(prefix+'/setup/login',{userName:user.username,password:user.password})
}
const getSonFile = function(parId,token){
    return axios.get(`${prefix}/file/getSonFile?fileId=${parId}`,{
        headers:{
            yToken:token
        }
    });
}
const getFileContent = function(fileId,token,encoding='GBK'){
    return axios.get(`${prefix}/oss/getFileSourceByFileId?fileId=${fileId}&encoding=${encoding}`,{
        headers:{
            yToken:token
        }
    })
}
const pushFileContent = function(fileId,content,token,decoding='GBK'){
    return axios.post(`${prefix}/oss/pushFileSource`,{content,fileId,decoding},{
        headers:{
            yToken:token
        }
    })
}
const saveFileNameById = function(fileId,fileName,token){
    return axios.post(`${prefix}/file/updateFileName`,{
        fileId,
        fileName
    },{
        headers:{
            yToken:token
        }
    })
}
const create_dir = function(fileFaId,token,suffix){
    return axios.post(`${prefix}/file/insertFile?afterName=${suffix?suffix:''}`,{
        fileFaId
    },{
        headers:{
            yToken:token
        }
    })
}
const create_desk_dir = function(fileFaId,x,y,token,suffix){
    return axios.post(`${prefix}/file/insertFile?afterName=${suffix?suffix:''}`,{
        fileFaId,
        x,
        y
    },{
        headers:{
            yToken:token
        }
    })
}
const create_file = function(fileFaId,token,suffix){
    return axios.post(`${prefix}/file/insertTextFile?afterName=${suffix?suffix:''}`,{
        fileFaId
    },{
        headers:{
            yToken:token
        }
    })
}
const create_desk_file = function(fileFaId,x,y,token,suffix){
    return axios.post(`${prefix}/file/insertTextFile?afterName=${suffix?suffix:''}`,{
        fileFaId,
        x,
        y
    },{
        headers:{
            yToken:token
        }
    })
}
const delete_file = function(fileId,token){
    return axios.get(`${prefix}/garbage/insertGarbag?fileId=${fileId}`,{
        headers:{
            yToken:token
        }
    })
}
const delete_dir = function(fileId,token){
    return axios.get(`${prefix}/garbage/insertMoreGarbags?fileId=${fileId}`,{
        headers:{
            yToken:token
        }
    })
}
const run_code = function(fileId,token){
    return axios.get(`${prefix}/run/runCode?fileId=${fileId}`,{
        headers:{
            yToken:token
        }
    })
}
const get_desk=  function(token){
    return axios.get(`${prefix}/file/getDesk`,{
        headers:{
            yToken:token
        }
    })
}
const getPer = function(token){
    return axios.get(`${prefix}/personality/getPersonality`,{
        headers:{
            yToken:token
        }
    })
}
const upload_file = function(faId,fileName,x,y,mtf,token){
    return axios.post(`${prefix}/file/insertMultipartFile?fileName=${fileName}&x=${x}&y=${y}&fileFaId=${faId}`,mtf,{
        headers:{
            yToken:token
        }
    })
}
const move_file = function(fileId,x,y,token){
    return axios.post(`${prefix}/file/moveDeskFile`,{
        fileId,
        x,
        y
    },{
        headers:{
            yToken:token
        }
    })
}
const get_file_detail = function(fileId,token){
    return axios.get(`${prefix}/file/getFile?fileId=${fileId}`,{
        headers:{
            yToken:token
        }
    })
}
const download_file =function(fileId,token){
    return axios.get(`${prefix}/file/downloadFile?fileId=${fileId}`,{
        headers:{
            yToken:token,
        },
        responseType:'blob'
    })
}
const upload_file_after =  function(fileId,isCoverage,mtf,token){
    return axios.post(`${prefix}/file/removeRepeat?fileId=${fileId}&isCoverage=${isCoverage}`,mtf,{
        headers:{
            yToken:token
        }
    })
}
const sortFile = function(token){
    return axios.get(`${prefix}/file/sortDesk`,{
        headers:{
            yToken:token
        }
    })
}
const create_room = function(roomName,token){
    return axios.post(`${prefix}/room/createRoom`,{roomName},{
        headers:{
            yToken:token
        }
    })
}
const get_room_list = function(token){
    return axios.get(`${prefix}/room/getRoomList`,{
        headers:{
            yToken:token
        }
    })
}
const pull_notice = function(token){
    return axios.get(`${prefix}/notice/pullNotice`,{
        headers:{
            yToken:token
        }
    })
}
const search_group_byName = function(name,token,pageNum = 1,pageSize=3){
    return axios.get(`${prefix}/room/searchRoom?roomName=${name}&pageNum=${pageNum}&pageSize=${pageSize}`,{
        headers:{
            yToken:token
        }
    })
}
const get_invite_noticec = function(token){
    return axios.get(`${prefix}/room/getInviteNotice`,{
        headers:{
            yToken:token
        }
    })
}
const getOwnerRoom = function(token){
    return axios.get(`${prefix}/room/getOwnerRoomList`,{
        headers:{
            yToken:token
        }
    })
}
const searchUserByName = function(userName,token){
    return axios.get(`${prefix}/user/getUserByName?userName=${userName}`,{
        headers:{
            yToken:token
        }
    })
}
const ms = {
    getUserInfo,
    userLogin,
    getSonFile,
    getFileContent,
    pushFileContent,
    saveFileNameById,
    delete_file,
    create_dir,
    create_file,
    run_code,
    get_desk,
    create_desk_file,
    delete_dir,
    create_desk_dir,
    getPer,
    upload_file,
    move_file,
    get_file_detail,
    download_file,
    upload_file_after,
    sortFile,
    create_room,
    get_room_list,
    pull_notice,
    search_group_byName,
    get_invite_noticec,
    getOwnerRoom,
    searchUserByName
}

const create_socket= function(token,onopen,onmessage,onclose,onerror){
    const url =`ws://172.30.233.181:9001/server/${token}`;
    const socket = new WebSocket(url);
    socket.onopen = onopen;
    socket.onclose = onclose
    socket.onerror = onerror
    socket.onmessage = onmessage
    return socket;
}
const invite_user_to_room = function(roomId,userId,token){
    return axios.get(`${prefix}/room/inviteUserToRoom?userId=${userId}&roomId=${roomId}`,{
        headers:{
            yToken:token
        }
    })
}
const send_msg_to_room = function(socket,msg){
    socket.send(JSON.stringify(msg))
}
const query_invite = function(token){
    return axios.get(`${prefix}/room/queryInvite`,{
        headers:{
            yTOken:token
        }
    })
}
const accept_room = function(roomId,token){
    return axios.get(`${prefix}/room/acceptRoom?roomId=${roomId}`,{
        headers:{
            yToken:token
        }
    })
}
const reject_room = function(roomId,token){
    return axios.get(`${prefix}/room/rejectRoom?roomId=${roomId}`,{
        headers:{
            yToken:token
        }
    })
}
const get_messages= function(roomId,pageNum,pageSize,token){
    return axios.get(`${prefix}/room/getMessages?roomId=${roomId}&pageNum=${pageNum}&pageSize=${pageSize}`,{
        headers:{
            yToken:token
        }
    })
}
const udpate_time = function(roomId,time,token){
    return axios.get(`${prefix}/room/pushLastTimestamp?roomId=${roomId}&time=${time}`,{
        headers:{
            yToken:token
        }
    })
}
const wbs = {
    create_socket,
    invite_user_to_room,
    send_msg_to_room,
    query_invite,
    accept_room,
    reject_room,
    get_messages,
    udpate_time
}
const getAllUser = function(token,pageNum=1,pageSize=10){
    return axios.get(`${prefix}/user/getAllUsers?pageNum=${pageNum}&pageSize=${pageSize}`,{
        headers:{
            yToken:token
        }
    })
}
const getTables = function(token){
    return axios.get(`${prefix}/database/getTablesAndDetail`,{
        headers:{
            yToken:token
        }
    })
}
const getLogs = function(token,pageNum,pageSize){
    return axios.get(`${prefix}/log/queryLog?pageNum=${pageNum}&pageSize=${pageSize}`,{
        headers:{
            yToken:token
        }
    })
}
const getWebsiteCount = function(token,start){
    return axios.get(`${prefix}/log/getNumInSec?start=${start}`,{
        headers:{
            yToken:token
        }
    })
}
const getWebsiteDay = function(token){
    return axios.get(`${prefix}/log/getNumByDay`,{
        headers:{
            yToken:token
        }
    })
}
const getLogCount = function(token){
    return axios.get(`${prefix}/log/getCount`,{
        headers:{
            yToken:token
        }
    })
}
const manage = {
    getAllUser,
    getTables,
    getLogs,
    getWebsiteCount,
    getWebsiteDay,
    getLogCount
}
export default {
    install(Vue){
        Vue.prototype.$ms = ms
        Vue.prototype.$prefix = prefix
        Vue.prototype.$websocket = wbs
        Vue.prototype.$manage = manage
        Vue.prototype.$basicAxios = basicAxios
    }
}